//
//  TZSMSLoginController.m
//  wangYiLottery
//
//  Created by mac on 16/2/20.
//  Copyright © 2016年 tz. All rights reserved.
//

#import "TZSMSLoginController.h"
#import "TZSuccessLoginController.h"
#import "TZNewUserController.h"
#import "TZNewPasswordController.h"
@interface TZSMSLoginController ()
@property (nonatomic, strong) UIView *phoneNumberView;
@property (nonatomic, strong) UIView *verificationView;
@property (nonatomic, strong) UIButton *verificationButton;
@property (nonatomic, strong) UIButton *passwordLoginButton;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIView *splitView;
@property (nonatomic, strong) UIButton *nextUserNameButton;
@property (nonatomic, strong) UIButton *nextPasswordButton;
@property (nonatomic, strong) UITextField *userNameTextField;
@property (nonatomic, strong) UITextField *passwordTextField;
@end

@implementation TZSMSLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.navigationItem setTitle:@"短信登录"];
    [self.view endEditing:YES];
    [self setupUI];
}

//更新UI
- (void)setupUI {
    [self addPhoneNumberInController];
    [self addVerificationInController];
    [self addSettingVerificationInController];
    [self addPasswordLoginInController];
    [self addLoginInController];
    [self addNewUserNameInController];
    [self addSplitLineInController];
    [self addNewPasswordInController];
}

//电话号码
- (void)addPhoneNumberInController {
    self.phoneNumberView = [[UIView alloc] init];
    [self.view addSubview:self.phoneNumberView];
    [self.phoneNumberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(30);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(KSCREEN_WIDTH * 0.8);
        make.height.mas_equalTo(40);
    }];
    UIImageView *wholeImageView = [[UIImageView alloc] init];
    [self.phoneNumberView addSubview:wholeImageView];
    [wholeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.phoneNumberView);
    }];
    wholeImageView.image = [UIImage imageNamed:@"yuan"];
    //内嵌手机图片
    UIImageView *thumbnailImageView = [[UIImageView alloc] init];
    [wholeImageView addSubview:thumbnailImageView];
    [thumbnailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wholeImageView.mas_left).offset(11);
        make.height.mas_equalTo(18.5);
        make.width.mas_equalTo(18);
        make.centerY.equalTo(wholeImageView.mas_centerY);
    }];
    thumbnailImageView.image = [UIImage imageNamed:@"shouji"];
    //内嵌textField
    UITextField *field = [[UITextField alloc] init];
    [self.phoneNumberView addSubview:field];
    [field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(thumbnailImageView.mas_right).offset(7);
        make.right.equalTo(wholeImageView.mas_right).offset(-8);
        make.centerY.equalTo(wholeImageView.mas_centerY);
        make.height.mas_equalTo(thumbnailImageView.mas_height);
    }];
    field.placeholder = @"请输入手机号码";
    field.font = [UIFont systemFontOfSize:12.0];
    field.textColor = [UIColor blackColor];
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.userNameTextField = field;
}

//验证码
- (void)addVerificationInController {
    self.verificationView = [[UIView alloc] init];
    [self.view addSubview:self.verificationView];
    [self.verificationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.phoneNumberView.mas_bottom).offset(20);
        make.left.equalTo(self.phoneNumberView.mas_left);
        make.height.mas_equalTo(self.phoneNumberView.mas_height);
        make.width.mas_equalTo(self.phoneNumberView.mas_width).multipliedBy(0.75);
    }];
    //内嵌整体图片
    UIImageView *wholeImageView = [[UIImageView alloc] init];
    [self.verificationView addSubview:wholeImageView];
    [wholeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.verificationView);
    }];
    wholeImageView.image = [UIImage imageNamed:@"yuan"];
    //内嵌手机图片
    UIImageView *thumbnailImageView = [[UIImageView alloc] init];
    [wholeImageView addSubview:thumbnailImageView];
    [thumbnailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(wholeImageView.mas_left).offset(11);
        make.height.mas_equalTo(18.5);
        make.width.mas_equalTo(18);
        make.centerY.equalTo(wholeImageView.mas_centerY);
    }];
    thumbnailImageView.image = [UIImage imageNamed:@"mima"];
    //内嵌textField
    UITextField *field = [[UITextField alloc] init];
    [self.verificationView addSubview:field];
    [field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(thumbnailImageView.mas_right).offset(7);
        make.right.equalTo(wholeImageView.mas_right).offset(-8);
        make.centerY.equalTo(wholeImageView.mas_centerY);
        make.height.mas_equalTo(thumbnailImageView.mas_height);
    }];
    field.placeholder = @"请输入验证码";
    field.font = [UIFont systemFontOfSize:12.0];
    field.textColor = [UIColor blackColor];
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.passwordTextField = field;
}

//获取验证码
- (void)addSettingVerificationInController {
    self.verificationButton = [[UIButton alloc] init];
    [self.view addSubview:self.verificationButton];
    [self.verificationButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.verificationView);
        make.height.mas_equalTo(self.verificationView.mas_height);
        make.right.equalTo(self.phoneNumberView.mas_right);
        make.width.mas_equalTo(self.phoneNumberView.mas_width).multipliedBy(0.2);
    }];
    [self.verificationButton setBackgroundImage:[UIImage imageNamed:@"yuanjiao1"] forState:UIControlStateNormal];
    [self.verificationButton setTitle:@"验证码" forState:UIControlStateNormal];
    [self.verificationButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.verificationButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    [self.verificationButton addTarget:self action:@selector(verificationButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
}

//获取验证码监听事件
- (void)verificationButtonDidClick {
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.userNameTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (error) {
            if (self.userNameTextField.text.length != 11) {
                [SVProgressHUD showWithStatus:@"手机号输入有误"];
            } else {
                [SVProgressHUD showWithStatus:@"验证码输入有误"];
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
        } else {
            [self.verificationButton startWithTime:60 title:nil countDownTitle:@"s" mainColor:nil countColor:nil];
        }
    }];
}

//密码登录
- (void)addPasswordLoginInController {
    self.passwordLoginButton = [[UIButton alloc] init];
    [self.view addSubview:self.passwordLoginButton];
    [self.passwordLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.verificationView.mas_bottom).offset(8);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(20);
    }];
    [self.passwordLoginButton setTitle:@"密码登录" forState:UIControlStateNormal];
    [self.passwordLoginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.passwordLoginButton.titleLabel.font = [UIFont systemFontOfSize:13.0];
    [self.passwordLoginButton addTarget:self action:@selector(passwordLoginButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
}

//密码登录的监听事件
- (void)passwordLoginButtonDidClick {
    [self.navigationController popViewControllerAnimated:YES];
}

//登录button
- (void)addLoginInController {
    self.loginButton = [[UIButton alloc] init];
    [self.view addSubview:self.loginButton];
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordLoginButton.mas_bottom).offset(8);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(self.phoneNumberView.mas_width);
        make.height.mas_equalTo(35);
    }];
    [self.loginButton setBackgroundImage:[UIImage imageNamed:@"RedButton"] forState:UIControlStateNormal];
    [self.loginButton setBackgroundImage:[UIImage imageNamed:@"RedButtonPressed"] forState:UIControlStateHighlighted];
    [self.loginButton setTitle:@"登陆" forState:UIControlStateNormal];
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.loginButton addTarget:self action:@selector(loginButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
}

//登录按钮的监听事件
- (void)loginButtonDidClick {
    //判断手机号是否输入正确
    if (self.userNameTextField.text.length != 11) {
        [SVProgressHUD showWithStatus:@"手机号码格式错误"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        return;
    }
    //判断验证码是否写入值
    if (self.passwordTextField.text.length == 0) {
        [SVProgressHUD showWithStatus:@"验证码格式错误"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        return;
    }
    //判断验证码是否输入正确
    [SMSSDK commitVerificationCode:self.passwordTextField.text phoneNumber:self.userNameTextField.text zone:@"86" result:^(NSError *error) {
        if (error) {
            [SVProgressHUD showWithStatus:@"验证码有误"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
            return ;
        } else {
            TZSuccessLoginController *successLoginController = [[TZSuccessLoginController alloc] init];
            [self.navigationController pushViewController:successLoginController animated:YES];
        }
    }];
}

//注册新账号
- (void)addNewUserNameInController {
    self.nextUserNameButton = [[UIButton alloc] init];
    [self.view addSubview:self.nextUserNameButton];
    [self.nextUserNameButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginButton.mas_bottom).offset(20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
        make.right.equalTo(self.view.mas_centerX).offset(-20);
    }];
    [self.nextUserNameButton setTitle:@"注册新账号" forState:UIControlStateNormal];
    [self.nextUserNameButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.nextUserNameButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [self.nextUserNameButton addTarget:self action:@selector(nextUserNameButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
}

//注册新账号的监听事件
- (void)nextUserNameButtonDidClick {
    TZNewUserController *newUserController = [[TZNewUserController alloc] init];
    [self.navigationController pushViewController:newUserController animated:YES];
}

//分割线
- (void)addSplitLineInController {
    self.splitView = [[UIView alloc] init];
    [self.view addSubview:self.splitView];
    [self.splitView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nextUserNameButton.mas_centerY);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(0.5);
        make.height.mas_equalTo(15);
    }];
    self.splitView.backgroundColor = [UIColor redColor];
}

//忘记密码
- (void)addNewPasswordInController {
    self.nextPasswordButton = [[UIButton alloc] init];
    [self.view addSubview:self.nextPasswordButton];
    [self.nextPasswordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.loginButton.mas_bottom).offset(20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
        make.left.equalTo(self.view.mas_centerX).offset(20);
    }];
    [self.nextPasswordButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    [self.nextPasswordButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.nextPasswordButton.titleLabel.font = [UIFont systemFontOfSize:12.0];
    [self.nextPasswordButton addTarget:self action:@selector(nextPasswordButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
}

//忘记密码的监听事件
- (void)nextPasswordButtonDidClick {
    TZNewPasswordController *newPasswordController = [[TZNewPasswordController alloc] init];
    [self.navigationController pushViewController:newPasswordController animated:YES];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
@end